package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;

/**
 * 作业列表-写评语
 */
@CrossOrigin
@RestController
public class WorkRemarkUpdController extends YxyApiBaseController {

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/remark/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        List<HomeworkTaskReport> homeworkTaskReports = homeworkTaskReportRepository
                .findByTaskIdAndClassIdAndStudentIdIn(reqModel.taskId, reqModel.classId, reqModel.studentIds);
        homeworkTaskReports.forEach(homeworkTaskReport -> {
            homeworkTaskReport.setEvaluate(reqModel.remark);
            homeworkTaskReportRepository.save(homeworkTaskReport);
        });
        return respModel;
    }

    /** 请求参数 */
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // classId
        public String classId;
        // 学生Ids
        public List<String> studentIds;
        // 评语 remark
        public String remark;
        // 用户Id (不用传参)
        public String userId;
        // 学校Id (不用传参)
        public String schoolId;
    }

    /** 响应 */
    public static class RespModel extends YxyApiRespModel {
    }
}
